const query = require('../../utils/DB')
class indexModel {
	static getMenu() {
		return query(`select * from menu where status=1`)
	}
	
	static getad(keyword){
		return query(`select a.*,b.* from ad_position as a,ad as b where a.keyword='${keyword}' and a.id=b.pid`)
	}
	
	// 首页商品 
	 static async getGoods(city='北京市',start=1,size=10){
		let nowPage = (start-1)*size
		let num = await query(`select count(*) as nums from goods where status=1 and position='${city}'`)
		let arr = await query(`select * from goods where status=1 and position='${city}' limit ${nowPage},${size}`)
		return {data:arr,sum:num[0][nums]}
	}
	
	// 商品详情
	static async getGoodsDetail(id){
		let res = await query(`select * from goods where id=${id} and status=1`)
		if(res.length==0){
			return false
		}
		let tagList = await query(`select * from tag where gid =${id}`)
		let skuList = await query(`select * from sku where gid=${id}`)
		let messageList = await query(`select * from message where gid=${id}`)
		return {...res[0],tagList,skuList,messageList}
	}
	
	// 首页商品
	 static async searchGoods(start=1,size=10,keyword=""){
		let nowPage = (start-1)*size
		let num = await query(`select count(*) as nums from goods where status=1 and name like "%${keyword}%" `)
		let arr = await query(`select * from goods where status=1 and name like "%${keyword}%" limit ${nowPage},${size}`)
		return {data:arr,sum:num[0]['nums']}
		// return {data:[],sum:10}
	}
}

module.exports = indexModel